package rest;

import org.apache.http.HttpHost;
import org.apache.http.HttpStatus;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;
import java.util.Collections;
import java.util.concurrent.ExecutionException;

/**
 * Created by Administrator on 2017/10/12.
 */
public class RestIndexClient {

    public RestClient getRestClient(){
        RestClient lowLevelRestClient = RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http"))
                .build();
        return lowLevelRestClient;
    }

    /**
     * 获取高级rest client
     * @return
     */
    public RestHighLevelClient getHighRestClient(){
        RestHighLevelClient client =  new RestHighLevelClient(getRestClient());
        return client;
    }
    /**
     * rest 创建索引
     * @param indexName
     */
    public  void createIndexWithRest(String indexName){
        String method = "PUT";
        String endpoint = "/"+indexName;
        Response response = null;
        try {
            response = getRestClient().performRequest(method, endpoint);
            if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {

            }
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     * rest删除索引
     *
     */
    /**
     * 创建索引
     * @param indexName
     */
    public  void delIndexWithRest(String indexName){
        String method = "DELETE";
        String endpoint = "/"+indexName;
        Response response = null;
        try {
            response = getRestClient().performRequest(method, endpoint);
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * es HTTP API 是典型的 RESTful API，所以 url 表示对资源的操作，例如 POST 新增，PUT 更新，GET 获取，DELETE 删除，HEAD 判断是否存在，此乃 REST API 之共性内容。除此之外，es 的 url 遵循一套规则，如：
     * 判断索引是否存在
     * @param indexName
     * @return
     */
    public  boolean isIndexExists(String indexName) {
        Response response = null;
        try {
            response = getRestClient().performRequest("HEAD", "/"+indexName, Collections.<String, String>emptyMap());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response.getStatusLine().getReasonPhrase().equals("OK");
    }

    /**
     * 判断表是否存在
     * @param indexName
     * @param typeName
     * @return
     * @throws InterruptedException
     * @throws java.util.concurrent.ExecutionException
     */
    public  boolean isTypeExists(String indexName, String typeName)
            throws InterruptedException, ExecutionException {
//        TypesExistsResponse typeExist = getClient().admin()
//                .indices().prepareTypesExists(indexName).setTypes(typeName).get();

        return false;
    }
}
